﻿Public Class Productos

    Dim descripcion, tipo As String
    Dim idProv, stock As Integer
    Dim precio As Double




    Private Sub Productos_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Me.CenterToScreen()
    End Sub

    Private Sub Productos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.AgendaTableAdapter.Fill(Me.NuevoZooDBDataSet.Agenda)

        Me.ClientesTableAdapter.Fill(Me.NuevoZooDBDataSet.Clientes)

        Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)

        descripcion = ""
        idProv = 0
        precio = 0
        tipo = ""
        stock = 0

    End Sub

   

    Private Sub BAddPro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BAddPro.Click
        Try
            descripcion = TxtAddDes.Text

            idProv = CInt(TxtAddProv.Text)

            precio = CDbl(Val(TxtAddPrecio.Text))

            tipo = TxtAddTipo.Text
            stock = CInt(TxtAddStock.Text)
       

       
        Dim query As IEnumerable(Of Decimal) = _
                From item As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
                Where item.Cells(0).Value IsNot DBNull.Value _
                Select CDec(item.Cells(0).Value)

        ' Obtenemos el valor máximo de id
        '
        Dim id As Integer = query.ToList.Max() + 1

        If Login.validar.ValidarNombre(descripcion) <> 3 Or descripcion = "" Then
            MsgBox("La descripción no es válida", , "Error")
        ElseIf Login.validar.ValidarNombre(tipo) <> 3 Or tipo = "" Then
            MsgBox("El tipo es incorrecto", , "Error")
        ElseIf stock = 0 Or Login.validar.ValidarInt(stock, 1, 1000) <> 3 Then
            MsgBox("El stock no es válido.", , "Error")
        ElseIf precio = 0 Then
            MsgBox("El precio no es válido.", , "Error")
        ElseIf id = 0 Then
            MsgBox("Debe seleccionar un registro de la tabla", , "Error")
        Else

            Try

                Me.ProductosTableAdapter.Insert(id, descripcion, idProv, tipo, stock, precio)
                MsgBox("Producto añadido correctamente", , "Registro guardado")

                Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)
                TabControl1.SelectedIndex = 0
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
            End If

        Catch ex As Exception
            MsgBox("Datos introducidos incorrectos", , "Error")
        End Try
    End Sub

    Private Sub DataGridView2_CellClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView2.SelectedRows(0)
        TxtAddProv.Text = dr.Cells(0).Value.ToString
    End Sub

    Private Sub DataGridView3_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView3.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)

        TxtID.Text = dr.Cells(0).Value.ToString
        TxtEdDes.Text = dr.Cells(1).Value.ToString
        TxtEdPrv.Text = dr.Cells(2).Value.ToString
        TxtTipo.Text = dr.Cells(3).Value.ToString
        TxtStock.Text = dr.Cells(4).Value.ToString
        TxtEdPrecio.Text = dr.Cells(5).Value.ToString

    End Sub

   
   
  
    Private Sub BEdProd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEdProd.Click
        Try

      
        Dim id As Integer

        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)
        id = dr.Cells(0).Value
        descripcion = TxtEdDes.Text
        idProv = TxtEdPrv.Text
        tipo = TxtTipo.Text
        Try
                precio = CDbl(Val(TxtEdPrecio.Text))
                stock = CInt(Val(TxtStock.Text))
        Catch ex As Exception
            MsgBox("Error al introducir los datos", , "Error")
        End Try


        If Login.validar.ValidarNombre(descripcion) <> 3 Or descripcion = "" Then
                MsgBox("Descripcion incorrecta", , "Error")
            ElseIf idProv = 0 Or Login.validar.ValidarInt(idProv, 1, 10000) <> 3 Then
                MsgBox("Debe seleccionar un proveedor", , "Error")
        ElseIf precio = 0 Then
                MsgBox("Precio incorrecto", , "Error")
            ElseIf stock = 0 Or Login.validar.ValidarInt(idProv, 1, 10000) <> 3 Then
                MsgBox("El stock tiene que ser al menos de 1", , "Error")
        Else


            Try
                Me.ProductosTableAdapter.UpdateQuery(descripcion, idProv, tipo, stock, precio, id)
                Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)
            Catch ex As Exception
                MsgBox("No se pudo guardar los datos")
            End Try
        End If

        Catch ex As Exception

        End Try
    End Sub


    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)

        TxtConId.Text = dr.Cells(0).Value.ToString
        TxtConDes.Text = dr.Cells(1).Value.ToString
        TxtConProv.Text = dr.Cells(2).Value.ToString
        TxtConTipo.Text = dr.Cells(3).Value.ToString
        TxtConSto.Text = dr.Cells(4).Value.ToString
        TxtConPrec.Text = dr.Cells(5).Value.ToString


    End Sub

    Private Sub BVenta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BVenta.Click
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)
        Dim cantidad As Integer = 0

        Try
            Dim id As Integer = dr.Cells(0).Value
            descripcion = TxtConDes.Text
            idProv = CInt(TxtConProv.Text)
            tipo = TxtConTipo.Text
            cantidad = CInt(Val(TxtCant.Text))
            stock = CInt(Val(TxtConSto.Text))
            precio = CDbl(Val(TxtConPrec.Text))
       



            If Login.validar.ValidarInt(cantidad, 1, 1000) <> 3 Or cantidad <= stock Then
                If cantidad <> 0 Then

                    stock = stock - cantidad
                    Me.ProductosTableAdapter.UpdateQuery(descripcion, idProv, tipo, stock, precio, id)
                    Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)
                    MsgBox("Producto vendido")
                Else
                    MsgBox("La cantidad no puede estar vacía", , "Error")
                End If
                limpiar()
            Else
                MsgBox("No hay suficiente stock", "Información")
            End If
        Catch ex As Exception
            MsgBox("Error al realizar la venta", , "Error")

        End Try



    End Sub


    Private Sub limpiar()
        TxtConId.Text = ""
        TxtConDes.Text = ""
        TxtConProv.Text = ""
        TxtConTipo.Text = ""
        TxtConSto.Text = ""
        TxtConPrec.Text = ""
        descripcion = ""




    End Sub
    Private Sub DataGridView4_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView4.CellClick
        Dim dr As New DataGridViewRow
        dr = DataGridView4.SelectedRows(0)

        TxtElID.Text = dr.Cells(0).Value.ToString
        TxtElDes.Text = dr.Cells(1).Value.ToString
        TxtElProv.Text = dr.Cells(2).Value.ToString
        TxtElTipo.Text = dr.Cells(3).Value.ToString
        TxtElStock.Text = dr.Cells(4).Value.ToString
        TxtElPrec.Text = dr.Cells(5).Value.ToString
    End Sub

    Private Sub BEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEliminar.Click
        Dim id As Integer = CInt(TxtElID.Text)
        If TxtElDes.Text <> "" Then
            Dim respuesta As MsgBoxResult = MsgBox("¿Desea eliminar el registro?", MsgBoxStyle.YesNo, "Confirmación")
            If respuesta = MsgBoxResult.Yes Then
                Try
                    Me.ProductosTableAdapter.Delete(id)
                    Me.ProductosTableAdapter.Fill(Me.NuevoZooDBDataSet.Productos)
                Catch ex As Exception
                    MsgBox("No se pudo borrar el registro", MsgBoxStyle.Critical, "Error")
                End Try

            Else

            End If
        Else
            MsgBox("Seleccione un registro para borrar.", MsgBoxStyle.OkOnly, "Datos incorrectos")
        End If
    End Sub

    Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
        Me.Close()
        Gestion.Show()

    End Sub

    Private Sub WebsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebsToolStripMenuItem.Click
        Webs.Show()
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        TabControl1.SelectedIndex = 3
    End Sub

    Private Sub EditarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        TabControl1.SelectedIndex = 2
    End Sub

   

    Private Sub NuevoToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NuevoToolStripMenuItem.Click
        TabControl1.SelectedIndex = 1
    End Sub

    Private Sub TextBox19_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox19.TextChanged
        Try
            Me.ProductosTableAdapter.FillByDescripcion(Me.NuevoZooDBDataSet.Productos, TextBox19.Text)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub TextBox18_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox18.TextChanged
        Try
            Me.ProductosTableAdapter.FillByTipo(Me.NuevoZooDBDataSet.Productos, TextBox18.Text)
        Catch ex As Exception

        End Try

    End Sub

    Private Sub AyudaToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AyudaToolStripMenuItem1.Click
        Dim rutaEjecutable As String = System.IO.Directory.GetCurrentDirectory().ToString
        System.Diagnostics.Process.Start(rutaEjecutable & "\Ayuda.chm")
    End Sub
End Class